 //
//  HomeOneImageCell.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/4.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "HomeOneImageCell.h"
#import "HomeSouceView.h"
#import "NewsDetailViewController.h"
#import "HomeTaskView.h"
#import "RouteViewController.h"
#import "AuditViewController.h"
#import "SpotTaskViewController.h"
#import "SpotCreateViewController.h"
#import "ChangeShiftViewController.h"

@interface HomeOneImageCell ()
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIImageView *iconView;
/// 时间 来源  标识
@property(nonatomic, strong)HomeSouceView *souceView;
@property(nonatomic, strong)HomeTaskView *taskView;

@end

@implementation HomeOneImageCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString * identifier = @"HomeOneImageCell";
    HomeOneImageCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HomeOneImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];

        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.font = [UIFont xm_medium:17.0];
        if (IS_IPHONE_5) {
            self.titleLabel.font = [UIFont xm_medium:13.0];

        }
        self.titleLabel.numberOfLines = 1;
        self.titleLabel.textColor = [UIColor xm_jetblackColor];
        [self addSubview:self.titleLabel];
        
        self.iconView = [[UIImageView alloc]init];
        [self addSubview:self.iconView];
        
        
        self.souceView = [[HomeSouceView alloc]init];
        [self addSubview:self.souceView];
        
        self.taskView = [[HomeTaskView alloc]init];
        [self addSubview:self.taskView];
        
        self.scanView = [[RouteScanView alloc]init];
        [self addSubview:self.scanView];
        
        self.scanView.hidden=YES;
        
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer)];
        [self addGestureRecognizer:tapGesture];
    }
    return self;
}

-(void)tapGestureRecognizer{
    if ([MySingleton sharedMySingleton].finished) {

    if (![_model.isAbnormal isEqualToString:@"1"]) {
        return;
    }
    }
    //时间问题
    NSDateFormatter *isoFormat = [[NSDateFormatter alloc] init];
    [isoFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
    
    
    NSDate*nowDate = [NSDate date];
    NSDate* theDate;
    

        theDate = nowDate;
    NSString* workStart=[self eightHour:theDate];

    if (_model.workOrderType==2) {
        //审查
            AuditViewController * detailVC = [[AuditViewController alloc]init];
            _model.workStartDate=workStart;
                 detailVC.model = _model;
        detailVC.block = ^(NSString *orderId, NSArray *lines) {
            if (self.blockAudit) {
                self.blockAudit(orderId, lines);
            }
        };
        detailVC.blockReject = ^{
            if (self.blockReject) {
                self.blockReject();
            }
        };
            [self.viewController.rt_navigationController pushViewController:detailVC animated:YES];
    }
//    else if (_model.workOrderType==3)
//    {
//        SpotCreateViewController* detailVC = [[SpotCreateViewController alloc]init];
//        //     detailVC.model = self.aticleArray[indexPath.row];
//        [self.viewController.rt_navigationController pushViewController:detailVC animated:YES];
//    }
    else if (_model.workOrderType==1)
    {
        ChangeShiftViewController* detailVC = [[ChangeShiftViewController alloc]init];
        _model.workStartDate=workStart;
             detailVC.model = _model;
        detailVC.block = ^(NSString *orderId, NSArray *lines) {
            if (self.blockShift) {
                self.blockShift(orderId, lines);
            }
        };
        detailVC.blockReject = ^{
            if (self.blockReject) {
                self.blockReject();
            }
        };
        [self.viewController.rt_navigationController pushViewController:detailVC animated:YES];
    }
    else if (_model.workOrderType==3)
    {
        SpotTaskViewController* detailVC = [[SpotTaskViewController alloc]init];
        _model.workStartDate=workStart;
             detailVC.model = _model;
        detailVC.block = ^(WorkOrderModel* m) {
            if (self.blockSpot) {
                self.blockSpot(m);
            }
        };
        detailVC.blockReject = ^{
            if (self.blockReject) {
                self.blockReject();
            }
        };
        detailVC.blockRejectSpot = ^{
            if (self.blockRejectSpot) {
                self.blockRejectSpot();
            }
        };
        
        [self.viewController.rt_navigationController pushViewController:detailVC animated:YES];
    }
    else{
    //巡查
//    NewsDetailViewController * detailVC = [[NewsDetailViewController alloc]init];
//    detailVC.model = self.model;
//    [self.viewController.rt_navigationController pushViewController:detailVC animated:YES];

    RouteViewController * detailVC = [[RouteViewController alloc]init];
    _model.workStartDate=workStart;
    detailVC.model = self.model;
        detailVC.block = ^(NSString *orderId, NSArray *lines) {
            if (self.block) {
                self.block(orderId, lines);
            }
            
        };
        detailVC.blockReject = ^{
            if (self.blockReject) {
                self.blockReject();
            }
        };
    [self.viewController.rt_navigationController pushViewController:detailVC animated:YES];
    }

}


-(void)setModel:(WorkOrderModel *)model{
    _model = model;
    NSString* type;
    if (model.workOrderType==0) {
        type=@"巡逻任务";
    }
    else if (model.workOrderType==1){
        type=@"交接班";
    }
    else if (model.workOrderType==2){
        type=@"审计";
    }
    else if (model.workOrderType==3){
        type=@"临时任务";
    }
        if ([_model.isAbnormal isEqualToString:@"1"]) {
            self.scanView.hidden=NO;
        }
    else
    {
        self.scanView.hidden=YES;

    }
    NSString* orderTitle=[NSString stringWithFormat:@"%@%@",type,model.workOrderNum];
    self.titleLabel.text = orderTitle;
    self.souceView.model = _model;
    self.taskView.model = _model;
    //
    RouteModel* r=[RouteModel new];
    r.ID=[@"1" integerValue];
    //设置异常工单红色图标
    if ([_model.isAbnormal isEqualToString:@"1"]) {
        r.ID=[@"100000" integerValue];
    }
    self.scanView.model=r;
    ;//    [self.iconView xm_setImageWithURL:_model.ImgUrl];
    switch (_model.workOrderType) {
        case 0:
            [self.iconView setImage:[UIImage imageNamed:@"icon-patrol"]];
            break;
        case 1:
            [self.iconView setImage:[UIImage imageNamed:@"icon-shift"]];
            break;
        case 2:
            [self.iconView setImage:[UIImage imageNamed:@"icon-inspect"]];
            break;
        case 3:
            [self.iconView setImage:[UIImage imageNamed:@"icon-task"]];
            break;
        default:
            [self.iconView setImage:[UIImage imageNamed:@"icon-task"]];
            break;
    }
}

+ (CGFloat)heightWithModel:(WorkOrderModel *)model{
    HomeOneImageCell *cell = [[HomeOneImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeOneImageCell"];
    cell.model = model;
    [cell layoutIfNeeded];
//    float c=CGRectGetMaxY(cell.souceView.frame) +20;
//    float d=CGRectGetMaxY(cell.iconView.frame) +10;
    
//    return CGRectGetMaxY(cell.souceView.frame) +20;
    if (IS_IPHONE_5||IS_IPHONE_6) {
        return CGRectGetMaxY(cell.iconView.frame) +30;
    }
    return CGRectGetMaxY(cell.iconView.frame) +20;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(18);
        make.left.equalTo(self.iconView.mas_right).offset(10);
    }];
    
    [self.souceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-20);
        make.top.mas_equalTo(self.titleLabel).offset(5);
        make.height.mas_equalTo(@60);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(20);
        make.top.mas_equalTo(self).offset(20);
        make.height.mas_equalTo(XMHeight(74));
        if (IS_IPAD) {
            make.height.mas_equalTo(74);

        }
        make.width.mas_equalTo(self.iconView.mas_height).multipliedBy(0.86);
    }];
    
    [self.taskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconView.mas_right).offset(10);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
//        make.right.equalTo(self.iconView.mas_left).offset(-20);
        make.height.mas_equalTo(XMHeight(74));
    }];
    
    [self.scanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-52);
        make.centerY.mas_equalTo(self).offset(0);
        //        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
    }];
}
-(NSString*)eightHour:(NSDate* )theDate
{
    //时区问题多8小时问题
    NSDateFormatter *isoFormat1 = [[NSDateFormatter alloc] init];
    [isoFormat1 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
    
    NSString *timeString = @"Sun Sep 13 11:43:04 +0800 2015";
    
    NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    
    format.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS";
    
    format.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    format.timeZone = sourceTimeZone;
    
    NSDate *date = [format dateFromString:[isoFormat1 stringFromDate:theDate]];
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    NSDate *localDate = [date dateByAddingTimeInterval:-interval];//减掉8小时，代码不得不这么写了
    
    NSLog (@"date: %@", localDate );
    
    
    //原来时间代码
    NSDateFormatter *aisoFormat = [[NSDateFormatter alloc] init];
    [aisoFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
    return [aisoFormat stringFromDate:localDate];
}

@end
